package com.example.checkqufuface.Customer;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.example.baselibrary.Base.MyApplication;
import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.SPUtils;
import com.example.checkqufuface.R;

/**
 * 作者：created by lenovoon 2020/3/19 15:10
 */
public class SettingDialog {

    private LinearLayout lLayout_bg;
    private Dialog dialog;
    private Context context;
    private Display display;
    private EditText IdIn;
    private Button btNeg;
    private Button btnPos;

    private boolean showTitle = false;
    private boolean showMsg = false;
    private boolean showPosBtn = false;
    private boolean showNegBtn = false;

    public SettingDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public String getInPut() {
        return IdIn.getText().toString().trim();
    }

    public Dialog getDialog() {
        return dialog;
    }

    public SettingDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(R.layout.setting_dialog, null);
        IdIn = (EditText) view.findViewById(R.id.et_input);
        btNeg = (Button) view.findViewById(R.id.btn_neg);
        btnPos = (Button) view.findViewById(R.id.btn_pos);
        lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);
        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 1), WindowManager.LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL));

        String BaseIP = (String) SPUtils.get(MyApplication.getAppContext(), Customer.QUFU_INPUT_URL, "");
        IdIn.setText(BaseIP);

        return this;
    }

    public SettingDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    ;

    public SettingDialog setPositiveButton(String text,
                                           final View.OnClickListener listener) {
        showPosBtn = true;
        if ("".equals(text)) {
            btnPos.setText("确定");
        } else {
            btnPos.setText(text);
        }
        btnPos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
//                getInPut();
//                dialog.dismiss();
            }
        });
        return this;
    }

    public SettingDialog setNegativeButton(String text,
                                           final View.OnClickListener listener) {
        showNegBtn = true;
        if ("".equals(text)) {
            btNeg.setText("取消");
        } else {
            btNeg.setText(text);
        }
        btNeg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
//                dialog.dismiss();
            }
        });
        return this;
    }

    public void show() {
        IdIn.setText((String) SPUtils.get(MyApplication.getAppContext(), Customer.QUFU_INPUT_URL, ""));
        dialog.show();
    }
}
